IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'CreateContact')
	BEGIN
		DROP  Procedure  CreateContact
	END

GO

CREATE Procedure CreateContact
(
	@accountId int,
	@guid nvarchar(200),
	@contactId int OUTPUT
)
AS

-- Check that account contact does not already exist.
SELECT @contactId = ContactId
	FROM Contacts
WHERE Guid = @guid AND AccountId = @accountId

IF @contactId IS NULL
BEGIN
	INSERT INTO Contacts (Guid, AccountId) VALUES (@guid, @accountId)
	SET @contactId = SCOPE_IDENTITY()
END

GO